package com.smq.chatserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smq.chatserver.bean.Friendship;
import com.smq.chatserver.bean.Result;
import com.smq.chatserver.bean.User;
import com.smq.chatserver.services.FriendshipService;
import com.smq.chatserver.services.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private FriendshipService friendshipService;


    @ApiOperation(value = "查询用户信息")
    @ApiImplicitParam(name = "userId", value = "用户id")
    @PostMapping("/info")
    public Result<User> getInfo(Integer userId) {
        return Result.success(userService.getOne(new QueryWrapper<User>().eq("user_id", userId)));
    }

    @PostMapping("/infoBatch")
    public Result<List<User>> infoBatch(List<String> userIds) {
        return Result.success();
    }

    @ApiOperation(value = "手机号-查询用户")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    @PostMapping("/getUserByMobile")
    public Result<User> getUserByMobile(String mobile) {
        return Result.success(userService.getOne(new QueryWrapper<User>().eq("mobile", mobile)));
    }

    @ApiOperation(value = "查询好友列表")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "header")
    @PostMapping("/whitelist/queryUser")
    public Result<List<User>> queryUser(@RequestHeader String userId) {
        return Result.success(friendshipService.queryUser(userId));
    }

    @ApiOperation(value = "添加好友")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "whiteUserId", value = "好友id"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "header")
    })
    @PostMapping("/whitelist/add")
    public Result<Object> add(Integer whiteUserId, @RequestHeader String userId) {
        boolean saved = friendshipService.save(new Friendship(null, Integer.parseInt(userId), whiteUserId, "accepted", null));
        return Result.with(saved);
    }

    @ApiOperation(value = "删除好友")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "whiteUserId", value = "好友id"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "header")
    })
    @PostMapping("/whitelist/remove")
    public Result<Object> remove(Integer whiteUserId, @RequestHeader String userId) {
        boolean removed = friendshipService.remove(new QueryWrapper<Friendship>().eq("user_id", userId).eq("friend_id", whiteUserId));
        return Result.with(removed);
    }
}
